goog.provide('bombernode.Game');

goog.require('lime.Sprite');
goog.require('bombernode.Player');

bombernode.Game = function(width, height) {
    lime.Sprite.call(this);

    this.WIDTH = width;
    this.HEIGHT = height;
    this.BLOCKHEIGHT = this.HEIGHT / ((bombernode.YBLOCKS*2)+1);
    this.BLOCKWIDTH = this.WIDTH / ((bombernode.XBLOCKS*2)+1);

    this.blocks = [];
    this.bombs = this.createBombArray();

    this.setFill('assets/dirt.jpg')
        .setSize(this.WIDTH, this.HEIGHT)
        .setAnchorPoint([0.5,0.5]);

    this.player = new bombernode.Player(3, 3, this);

    this.drawHardBlocks();
    this.drawBlocks();

    this.appendChild(this.player);

    this.start();
}
goog.inherits(bombernode.Game, lime.Sprite);

bombernode.Game.prototype.start = function() {
    lime.scheduleManager.schedule(this.step_, this);
};

bombernode.Game.prototype.step_ = function(dt) {
    this.player.move(dt);
};

bombernode.Game.prototype.createBombArray = function()
{
    var xSize = bombernode.XBLOCKS * 2 + 1;
    var ySize = bombernode.YBLOCKS * 2 + 1;
    var array = new Array(xSize);
    for(var x = 0; x < xSize; x++)
    {
        array[x] = new Array(ySize);
    }
    return array;
}

bombernode.Game.prototype.drawHardBlocks = function() {
    for(var x = this.BLOCKWIDTH + this.BLOCKWIDTH/2; x <= this.WIDTH; x += this.BLOCKWIDTH * 2)
    {
        for(var y = this.BLOCKHEIGHT + this.BLOCKHEIGHT/2; y <= this.HEIGHT; y += this.BLOCKHEIGHT * 2)
        {
            var block = new lime.Sprite()
                .setFill('#ccc')
                .setSize(this.BLOCKWIDTH, this.BLOCKHEIGHT)
                .setPosition(x, y);
            this.appendChild(block);
        }
    }
}

bombernode.Game.prototype.drawBlocks = function() {

    for(var x = 0; x < this.WIDTH; x += this.BLOCKWIDTH)
    {
        for(var y = 0; y <= this.HEIGHT; y += this.BLOCKHEIGHT * 2)
        {
            if(Math.floor(Math.random()*101) < bombernode.BLOCKPOSSIBILITY)
            {
                this.putBlock(x, y);
            }
        }
    }
    for(var y = 0 + this.BLOCKHEIGHT; y <= this.HEIGHT; y += this.BLOCKHEIGHT * 2)
    {
        for(x = 0; x <= this.WIDTH; x += this.BLOCKWIDTH * 2)
        {
            if(Math.floor(Math.random()*101) < bombernode.BLOCKPOSSIBILITY)
            {
                this.putBlock(x, y);
            }
        }
    }
}

bombernode.Game.prototype.putBlock = function(x, y)
{
    var block = new lime.Sprite()
        .setFill('#fff')
        .setSize(this.BLOCKWIDTH, this.BLOCKHEIGHT)
        .setPosition(x, y)
        .setAnchorPoint(0, 0);
    this.appendChild(block);
    if(this.player.hasCollision(block))
    {
        block.setSize(0,0); //TODO: remove from DOM etc, but how?!
    }
    else
    {
        this.blocks.push(block);
    }
}

bombernode.Game.prototype.getCellIndex = function(x, y)
{
    var xIndex = Math.floor((x) / this.BLOCKWIDTH);
    var yIndex = Math.floor((y) / this.BLOCKHEIGHT);
    return new goog.math.Coordinate(xIndex, yIndex);
}
